package com.jxzyk.model;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.core.entity.params.MapParams;
import com.core.entity.params.ReqParams;
import com.core.ui.activity.BaseActivity;
import com.core.util.T;
import com.core.util.UiTools;
import com.jxzyk.R;
import com.jxzyk.config.API;
import com.jxzyk.entity.QAList;
import com.jxzyk.entity.User;
import com.jxzyk.http.BaseRequest;
import com.jxzyk.http.BaseRequest.OnDialogListener;
import com.jxzyk.http.req.NotesRequest;
import com.jxzyk.http.req.QARequest;
import com.jxzyk.util.UserManger;
import com.jxzyk.view.TitleLayout;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.act_edit_note)
public class EditActivity extends BaseActivity {
	@ViewInject(R.id.title)
	TitleLayout title;
	@ViewInject(R.id.et_edit_title)
	EditText et_title;
	@ViewInject(R.id.et_edit_content)
	EditText et_content;
	@ViewInject(R.id.tv_know_qa)
	TextView tv_qa;
	private String url = "";
	private UserManger manger;
	private String id;// id编码
	private QAList qaList;

	public void setVisibility(int visibility) {
		et_title.setVisibility(visibility);
	}

	public String getEt_Title() {
		return UiTools.getString(et_title);
	}

	public String getEt_Content() {
		return UiTools.getString(et_content);
	}

	@Override
	public void initData(Bundle bundle) {
		bundle = getIntent().getExtras();
		if (bundle != null) {
			url = bundle.getString(EditActivity.class.getName());
			switch (url) {
			case API.QA.answer:
				qaList = (QAList) bundle
						.getSerializable(QAList.class.getName());
				id = qaList.getQa_id();
				break;
			case API.Notes.add:
				id = bundle.getString("outline_id");
				break;
			default:
				break;
			}
		}
		manger = new UserManger(getBaseContext());
	}

	public void setTitle(String titleString) {
		title.setTtile(titleString);
	}

	public void setTitleRight(String rightString, OnClickListener listener) {
		title.setOnRightListener(rightString, listener);
	}

	@Override
	public void initUi() {
		switch (url) {
		case API.QA.publish:// 发布问题
			setTitle("发布问题");
			setVisibility(View.VISIBLE);
			break;
		case API.QA.answer:// 回答问题
			setTitle("回答问题");
			tv_qa.setVisibility(View.VISIBLE);
			tv_qa.setText(qaList.getQa_content());
			break;
		case API.Notes.add:// 增加笔记
			setTitle("新建笔记");
			break;
		default:
			break;
		}

		setTitleRight("保存", new OnClickListener() {

			@Override
			public void onClick(View v) {
				String title = getEt_Title();
				String content = getEt_Content();
				if (!UiTools.Empty(content)) {
					T.toast("请输入内容再保存");
					return;
				}
				try {
					saveQuestion(title, content);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	BaseRequest request;

	private void saveQuestion(String title, String content) throws Exception {
		ReqParams params = new MapParams();
		User user = manger.getUser();
		switch (url) {
		case API.QA.publish:// 发布问题
			request = new QARequest(EditActivity.this, url);
			params.put("title", title);
			params.put("content", content);
			params.put("userid", user.getUser_id());
			break;
		case API.QA.answer:
			request = new QARequest(EditActivity.this, url);
			params.put("userid", user.getUser_id());
			params.put("qa_id", id);
			params.put("answer", content);
			break;
		case API.Notes.add:
			request = new NotesRequest(EditActivity.this, url);
			params.put("user_id", user.getUser_id());
			params.put("outline_id", id);
			params.put("note", content);
			break;
		default:
			break;
		}
		request.setParams(params);
		request.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				String message = json.getString("message");
				if (UiTools.Empty(message)) {
					T.toast(message);
				}
				finish();
			}

			@Override
			public void onFailure(String msg) {

			}
		});
		request.show("正在保存...");
	}
}
